﻿using Newtonsoft.Json;
using BuyingSpree_HPV.Model;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace BuyingSpree_HPV.Help
{
    public class HttpService
    {
        private static string baseUrl = "https://miaomiao.scmttec.com";

        /// <summary>
        /// 获取秒杀资格
        /// </summary>
        /// <param name="seckillId">疫苗ID</param>
        /// <param name="vaccineIndex">固定1</param>
        /// <param name="linkmanId">接种人ID</param>
        /// <param name="idCard">接种人身份证号码</param>
        /// <returns>返回订单ID</returns>
        public static string SecKill(int seckillId, string vaccineIndex, string linkmanId, string idCard)
        {
            string path = baseUrl + "/seckill/seckill/subscribe.do";
            Dictionary<string, object> keyValues = new Dictionary<string, object>
            {
                { "seckillId", seckillId},
                { "vaccineIndex", vaccineIndex },
                { "linkmanId", linkmanId },
                { "idCardNo", idCard}
            };

            //后面替换成接口返回的st
            //目前发现接口返回的st就是当前时间，后面可能会固定为一个加密参数
            long milliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>
            {
                { "ecc-hs", CcHs(seckillId.ToString(), milliseconds) }
            };
            ServiceResponse response = Network.HttpGet(path, keyValues, keyValuePairs);
            return string.Empty;
        }

        /// <summary>
        /// 获取接种人信息
        /// </summary>
        /// <returns></returns>
        public static List<Member> GetMembers()
        {
            string path = baseUrl + "/seckill/linkman/findByUserId.do";
            ServiceResponse response = Network.HttpGet(path);
            return JsonConvert.DeserializeObject<List<Member>>(response.data.ToString()); ;
        }
        /// <summary>
        /// 获取疫苗列表
        /// </summary>
        /// <returns></returns>
        public static List<VaccineList> GetVaccineList()
        {
            //hasAvailableConfig();
            string path = baseUrl + "/seckill/seckill/list.do";
            Dictionary<string, object> keyValues = new Dictionary<string, object>
            {
                //九价疫苗的code
                { "offset", "0" },
                { "limit", "100" },
                //这个应该是成都的行政区划前四位
                { "regionCode", Config.HeaderConfig.regionCode }
            };
            //ServiceResponse response = Network.HttpGet(path, keyValues);
            string json = "[{\"id\":915,\"name\":\"龙泉驿区十陵社区卫生服务中心\",\"imgUrl\":\"https://adultvacc-1253522668.image.myqcloud.com/thematic%20pic/%E5%8D%81%E9%99%B5%E7%A4%BE%E5%8C%BA%E5%8D%AB%E7%94%9F%E6%9C%8D%E5%8A%A1%E4%B8%AD%E5%BF%83%E5%AE%A3%E4%BC%A0%E5%9B%BE_1513242665704.png?imageView2/1/w/200/format/jpg\",\"vaccineCode\":\"8803\",\"vaccineName\":\"九价HPV疫苗（进口）\",\"address\":\"龙泉驿区十陵街道友谊路393号.\",\"startTime\":\"2021-03-26 14:00:00\",\"stock\":1}]";
            return JsonConvert.DeserializeObject<List<VaccineList>>(json);
        }

        /// <summary>
        /// 获取加密参数st
        /// </summary>
        /// <param name="vaccineId">疫苗ID</param>
        /// <returns></returns>
        public static string GetSt(string vaccineId)
        {
            string path = baseUrl + "/seckill/seckill/checkstock2.do";
            Dictionary<string, object> keyValues = new Dictionary<string, object>
            {
                //九价疫苗的code
                { "id", vaccineId },
                { "limit", "100" },
                //这个应该是成都的行政区划前四位
                { "regionCode", Config.HeaderConfig.regionCode }
            };
            ServiceResponse response = Network.HttpGet(path, keyValues);
            return "";
        }
        public static void hasAvailableConfig()
        {
            if (string.IsNullOrWhiteSpace(Config.HeaderConfig.cookies))
            {
                Exception ex = new Exception("请先配置cookie");
                throw ex;
            }
        }

        private static string CcHs(string seckillId, long st)
        {
            string salt = "ux$ad70*b";
            int memberId = Config.HeaderConfig.memberId;
            string md5 = GenerateMD5(seckillId + st + memberId);
            return GenerateMD5(md5 + salt);
        }

        /// <summary>
        /// MD5字符串加密
        /// </summary>
        /// <param name="txt"></param>
        /// <returns>加密后字符串</returns>
        private static string GenerateMD5(string txt)
        {
            using (MD5 mi = MD5.Create())
            {
                byte[] buffer = Encoding.Default.GetBytes(txt);
                //开始加密
                byte[] newBuffer = mi.ComputeHash(buffer);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < newBuffer.Length; i++)
                {
                    sb.Append(newBuffer[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

        

    }
}

